package com.xyy.saas.payment.service.impl;

import com.xyy.saas.payment.dao.mapper.RiskControlPayWhitelistMapper;
import com.xyy.saas.payment.dao.model.RiskControlPayWhitelist;
import com.xyy.saas.payment.dao.model.RiskControlPayWhitelistQueryParam;
import com.xyy.saas.payment.service.RiskControlPayWhitelistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
@Slf4j
public class RiskControlPayWhitelistServiceImpl implements RiskControlPayWhitelistService {

    @Autowired
    private RiskControlPayWhitelistMapper riskControlPayWhitelistMapper;

    @Override
    public Long getTotal(RiskControlPayWhitelistQueryParam param) {
        return riskControlPayWhitelistMapper.getTotal(param);
    }

    @Override
    public int batchAdd(List<RiskControlPayWhitelist> entities) {
        if(null == entities || entities.isEmpty()) {
            return 0;
        }
        AtomicInteger rows = new AtomicInteger();
        // 存在更新有效时间，不存在直接插入
        entities.stream().forEach(entity -> {
            RiskControlPayWhitelistQueryParam param = new RiskControlPayWhitelistQueryParam();
            param.setBusinessType(entity.getBusinessType());
            param.setPayerId(entity.getPayerId());
            List<RiskControlPayWhitelist> riskControlPayWhitelists = pageListByParam(param);
            if(riskControlPayWhitelists.isEmpty()) {
                rows.addAndGet(riskControlPayWhitelistMapper.insert(entity));
                return;
            }
            riskControlPayWhitelists.stream().forEach(item -> {
                RiskControlPayWhitelist record = new RiskControlPayWhitelist();
                record.setId(item.getId());
                record.setPayerPhone(entity.getPayerPhone());
                record.setPayerName(entity.getPayerName());
                record.setChannelRule(entity.getChannelRule());
                record.setStartTime(entity.getStartTime());
                record.setEndTime(entity.getEndTime());
                record.setUpdateTime(new Date());
                rows.addAndGet(riskControlPayWhitelistMapper.updateByPrimaryKeySelective(record));
            });
        });
        return rows.get();
    }

    @Override
    public List<RiskControlPayWhitelist> pageListByParam(RiskControlPayWhitelistQueryParam param) {
        if(null == param) {
            return Collections.emptyList();
        }
        if(param.getPage() != null) {
            if(param.getPageSize() != null && param.getPageSize() > 0) {
                param.setOffset((param.getPage() - 1) * param.getPageSize());
            } else {
                return Collections.emptyList();
            }
        }
        return riskControlPayWhitelistMapper.pageListByParam(param);
    }

    @Override
    public int deleteById(Long id) {
        return riskControlPayWhitelistMapper.deleteByPrimaryKey(id);
    }

}
